/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class DateUtil {

    static Logger logger = Logger.getLogger(DateUtil.class.getName());

    public static String getYesterdayDate() {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
            Calendar calendar = Calendar.getInstance(); //得到日历
            calendar.setTime(new Date());//把当前时间赋给日历
            calendar.add(Calendar.DAY_OF_MONTH, -1);  //设置为前一天
            String yesterdayDate = df.format(calendar.getTime());
            return yesterdayDate;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return "";
        }

    }

    public static String getToDay_Date() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String todayDate = df.format(new Date());
        return todayDate;
    }

    public static String getToDayDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
        String todayDate = df.format(new Date());
        return todayDate;
    }

    public static String transformDateFormate(String date) {
        Calendar calendar = Calendar.getInstance(); //得到日历
        String year = date.substring(0, 4);
        String month = date.substring(4, 6);
        String day = date.substring(6, 8);
        System.out.println(year + "\t" + month + "\t" + day);
        System.out.println(Integer.parseInt(year) + "\t" + Integer.parseInt(month) + "\t" + Integer.parseInt(day));
        calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));//把当前时间赋给日历
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String dateF = df.format(calendar.getTime());
        System.out.println(dateF);
        return dateF;
    }

    public static String getTheDayBeforeDate() {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
            Calendar calendar = Calendar.getInstance(); //得到日历
            calendar.setTime(new Date());//把当前时间赋给日历
            calendar.add(Calendar.DAY_OF_MONTH, -2);  //设置为前一天
            String yesterdayDate = df.format(calendar.getTime());
            return yesterdayDate;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return "";
        }
    }

    public static void main(String[] args) {
        logger.info(getToDay_Date());
        transformDateFormate(getTheDayBeforeDate());
    }
}
